/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI.DocumentPanel;

import Common.IDocument;
import Common.IUnit;
import ComponentsUI.IPrimitiveUI;
import Primitive.EPrimitiveType;
import Project.PrimitiveModelFactory;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;

/**
 *
 * @author Администратор
 */
public class DocumentPanel extends JPanel {

    private IDocument document;
    private Point firstPoint, lastPoint;

    public DocumentPanel(IDocument document) {
        this.document = document;
        firstPoint = new Point();
        lastPoint = new Point();
        for (IUnit unit : this.document.getUnits()) {
            if (unit instanceof IPrimitiveUI) {
                IPrimitiveUI primitive = (IPrimitiveUI) unit;
                primitive.Draw();
            }
        }

        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void mousePressed(MouseEvent e) {
                firstPoint.x = e.getX();
                firstPoint.y = e.getY();
                panelMousePressed(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                lastPoint.x = e.getX();
                lastPoint.y = e.getY();
                CreatePrimitiveUI(EPrimitiveType.Rectangle);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                panelMouseEntered(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                //throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    public IDocument getDocument() {
        return this.document;
    }

    private void panelMousePressed(MouseEvent e) {
        System.out.println("Нажата конпка мыши на панели " + this.document.getName());
    }

    private void panelMouseEntered(MouseEvent e) {
        System.out.println("Нажата конпка мыши на панели " + this.document.getName());
    }

    private void CreatePrimitiveUI(EPrimitiveType primitiveType)
    {
        IPrimitiveUI primitive = new PrimitiveModelFactory().CreatePrimitiveModel(this.document,primitiveType,firstPoint.x,firstPoint.y);
        primitive.setGraphics(this.getGraphics());
        primitive.setPosition(firstPoint);
        primitive.setSize(lastPoint.x-firstPoint.x, lastPoint.y-firstPoint.y);
        primitive.Draw();
        this.document.getUnits().add(primitive);
    }
}
